package cn.ciis.sys.role;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Record;

import cn.ciis.basic.auth.AdminAuthService;
import cn.ciis.basic.base.BaseController;
import cn.ciis.basic.kit.CiisKit;
import cn.ciis.basic.model.Permission;
import cn.ciis.basic.model.Role;
import cn.ciis.sys.permission.Notes;
import cn.ciis.sys.permission.PermissionAdminService;
import cn.ciis.sys.permission.Remark;
import cn.hutool.core.date.DateUtil;

/**
 * 角色管理控制器
 *
 * @author 冉志林
 */
public class RoleAdminController extends BaseController {

    @Inject
   RoleAdminService srv;

    @Inject
    PermissionAdminService permissionAdminSrv;

    @Inject
    AdminAuthService adminAuthService;

    @Notes(value = "角色管理")
    @Remark(value = "角色列表")
    public void index() {
        Role mode = getModel(Role.class, "", true);
        boolean b = adminAuthService.isSuperAdmin(getLoginAccountId());
        if (!b) {
            mode.set("id", 1);
        }
        kModel(mode, srv.paginate(getInt(PAGENUMBER, 1), getInt(PAGESIZE, 10), mode));
    }

    @Remark(value = "角色添加或编辑页")
    public void addOrEdit(Integer id) {
        kAddOrEdit(srv.findById(id));
    }

    @Remark(value = "角色保存或修改")
    @Before(RoleAdminValidator.class)
    public void saveOrUpdate() {
        Role bean = getBean(Role.class);
        bean.setCreateAt(new Date());
        renderJson(srv.saveOrUpdate(bean));
    }

    @Remark(value = "角色删除")
    public void deletes(String ids) {
        if (ids.indexOf("1,") != 0 && !"1".equals(ids)) {
            renderJson(srv.deletes(ids));
        } else {
            renderJson(srv.fail());
        }
    }

    @Remark(value = "角色分配权限")
    public void assignPermissions(String id) {
        String modes = getPara("mode");
        Role role = (Role) srv.findById(id);
        boolean b = adminAuthService.isSuperAdmin(getLoginAccountId());
        List<Permission> permissionList = permissionAdminSrv.getAllPermissions(!b ? id : null);
        srv.markAssignedPermissions(role, permissionList);
        LinkedHashMap<String, List<Permission>> permissionMap =
                srv.groupByController(permissionList);
        setAttr("role", role);
        setAttr("permissionMap", permissionMap);
        List<Record> actions = srv.listActionNotes(null);
        set("actions", actions);
        if (modes != null) {
            List<Record> attributeValue = srv.listActionNotes(modes);
            set("actionNotes", attributeValue);
        } else {
            set("actionNotes", new ArrayList<>());
            set("notes", actions);
        }
        render("assign_permissions.html");
    }

    @Remark(value = "添加权限")
    public void addPermission() {
        Ret ret = srv.addPermission(getParaToInt("roleId"), get("permissionId"));
        renderJson(ret);
    }

    @Remark(value = "删除权限")
    public void deletePermission() {
        Ret ret = srv.deletePermission(getParaToInt("roleId"), get("permissionId"));
        renderJson(ret);
    }
}
